package com.qc.serviceedu.controller;


import com.qc.commonutils.result.R;
import com.qc.serviceedu.client.VodClient;
import com.qc.serviceedu.entity.EduChapter;
import com.qc.serviceedu.entity.EduVideo;
import com.qc.serviceedu.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author AltmanQc
 * @since 2020-12-25
 */
@ApiModel(value = "小节管理")
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小结
    @ApiOperation("添加小节")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo video){
        videoService.save(video);
        return R.ok();
    }

    //根据小结id查询小节信息
    @ApiOperation("根据查询小节信息")
    @GetMapping("getVideoById/{videoId}")
    public R getVideoById(@PathVariable String videoId){
        EduVideo video = videoService.getById(videoId);
        return R.ok().data("video",video);
    }

    //修改小结
    @ApiOperation("修改小节信息")
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo video){
        videoService.updateById(video);
        return R.ok();
    }

    //删除小结
    @ApiOperation("删除章节信息")
    @DeleteMapping("{videoId}")
    public R deleteChapter(@PathVariable String videoId){
        //先根据小节id获取视频id，调用方法实现删除
        String sourceId = videoService.getById(videoId).getChapterId();
        //判断小节里面是否有视频id
        if (!StringUtils.isEmpty(sourceId)){
            //根据视频id，实现远程过程调用
            vodClient.removeVideo(sourceId);
        }
        videoService.removeById(videoId);
        return R.ok();
    }

    //删除小结，删除对应阿里云视频
    @ApiOperation("删除小节信息")
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id){
        //先根据小节id获取视频id，调用方法实现删除
        String sourceId = videoService.getById(id).getVideoSourceId();
        //判断小节里面是否有视频id
        if (!StringUtils.isEmpty(sourceId)){
            //根据视频id，实现远程过程调用
            vodClient.removeVideo(sourceId);
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }

}

